﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DOTNETGROUP.Data;
using System.Text;
using DOTNETGROUP.Entities;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;

public partial class Controls_ControlMauWebContent : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            HienThiChuyenMuc(Pager1.CurrentIndex);
            Pager2.CurrentIndex = 1;
            HienThiChuyenMucThuHai(Pager2.CurrentIndex);
            Pager3.CurrentIndex = 1;
            HienThiChuyenMucThuBa(Pager3.CurrentIndex);
        }
    }

    protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
       
        Rating thisRating = (Rating)sender;

        HiddenField H_Id = (HiddenField)thisRating.FindControl("H_catalogId");

        string query = string.Empty;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        conn.Open();
        query = "UPDATE MauWeb SET DiemSo = DiemSo +'" + e.Value.ToString() + "' where Id = '" + H_Id.Value + "'";
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = conn;
        sqlCommand.CommandText = query;
        sqlCommand.ExecuteNonQuery();
        ShowPopUpMsg(Constants.BINHCHON_THANHCONG);
        WriteCookie(H_Id.Value);
    }

    protected void Rating2_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {

        Rating thisRating = (Rating)sender;

        HiddenField H_Id = (HiddenField)thisRating.FindControl("H_catalogId2");

        string query = string.Empty;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        conn.Open();
        query = "UPDATE MauWeb SET DiemSo = DiemSo +'" + e.Value.ToString() + "' where Id = '" + H_Id.Value + "'";
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = conn;
        sqlCommand.CommandText = query;
        sqlCommand.ExecuteNonQuery();
        ShowPopUpMsg(Constants.BINHCHON_THANHCONG);
        WriteCookie(H_Id.Value);
    }
    protected void Rating3_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {

        Rating thisRating = (Rating)sender;

        HiddenField H_Id = (HiddenField)thisRating.FindControl("H_catalogId3");

        string query = string.Empty;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        conn.Open();
        query = "UPDATE MauWeb SET DiemSo = DiemSo +'" + e.Value.ToString() + "' where Id = '" + H_Id.Value + "'";
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = conn;
        sqlCommand.CommandText = query;
        sqlCommand.ExecuteNonQuery();
        ShowPopUpMsg(Constants.BINHCHON_THANHCONG);
        WriteCookie(H_Id.Value);
    }

    protected void WriteCookie(string H_id)
    {
        HttpCookie cookieBaiViet = new HttpCookie(H_id);

        cookieBaiViet.Expires = DateTime.Now.AddMinutes(30);
        Response.Cookies.Add(cookieBaiViet);
    }

    /// <summary>
    /// Phuong thuc Show MESSAGE
    /// </summary>
    /// <param name="msg"></param>
    private void ShowPopUpMsg(string msg)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("alert('");
        sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
        sb.Append("');");
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
    }

    private void HienThiChuyenMuc(int PageNo)
    {

        int TotalRows = 0;
        string WhereClause = " TrangThai = 1 AND IdLoaiMauWeb=1";
        DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("MauWeb_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
        dlChuyenMuc1.DataSource = dsTinTuc;
        dlChuyenMuc1.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        HienThiChuyenMuc(currnetPageIndex);
    }

    private void HienThiChuyenMucThuHai(int PageNo)
    {

        int TotalRows = 0;
        string WhereClause = " TrangThai = 1 AND IdLoaiMauWeb=2";
        DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("MauWeb_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager2.PageSize);
        dlChuyenMuc2.DataSource = dsTinTuc;
        dlChuyenMuc2.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager2.ItemCount = TotalRows;
        if (TotalRows <= Pager2.PageSize)
            Pager2.Visible = false;
        else
            Pager2.Visible = true;
    }

    protected void Pager2_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager2.CurrentIndex = currnetPageIndex;
        HienThiChuyenMucThuHai(currnetPageIndex);
    }

    private void HienThiChuyenMucThuBa(int PageNo)
    {

        int TotalRows = 0;
        string WhereClause = " TrangThai = 1 AND IdLoaiMauWeb=3";
        DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("MauWeb_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager3.PageSize);
        dlChuyenMuc3.DataSource = dsTinTuc;
        dlChuyenMuc3.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsTinTuc.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager3.ItemCount = TotalRows;
        if (TotalRows <= Pager3.PageSize)
            Pager3.Visible = false;
        else
            Pager3.Visible = true;
    }

    protected void Pager3_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager3.CurrentIndex = currnetPageIndex;
        HienThiChuyenMucThuBa(currnetPageIndex);
    }

    public int diemSo(object Char)
    {
        int id = Convert.ToInt32(Char.ToString());
        MauWeb ent_mauWeb = DataRepository.MauWebProvider.GetById(id);
        if (ent_mauWeb.DiemSo > 1 && ent_mauWeb.DiemSo<49)
            return 1;
        if (ent_mauWeb.DiemSo > 50 && ent_mauWeb.DiemSo < 99)
            return 2;
        if (ent_mauWeb.DiemSo > 100 && ent_mauWeb.DiemSo < 199)
            return 3;
        if (ent_mauWeb.DiemSo > 200 && ent_mauWeb.DiemSo < 299)
            return 4;
        if (ent_mauWeb.DiemSo > 300 && ent_mauWeb.DiemSo < 500)
            return 5;
        return 0;
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
}